package com.bdqn.controller;

import com.bdqn.pojo.StorageRecord;
import com.bdqn.pojo.Supplier;
import com.bdqn.service.StorageRecordService;
import com.bdqn.service.SupplierService;
import com.bdqn.util.Constants;
import com.bdqn.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/storageRecord") // 路径
public class StorageRecordController {
    @Resource
    private StorageRecordService storageRecordService;
    @Resource
    private SupplierService supplierService;
    @GetMapping("/list")
    public String toList(Model model, String queryGoodsName, Integer querySupplierId, Integer queryPayStatus, @RequestParam(defaultValue = "1")Integer pageIndex){
        List<StorageRecord> storageRecords = null;
        List<Supplier> supplierList = null;
        try{
            //设置页面容量
            int pageSize = Constants.pageSize;
            //总数量
            int totalCount = storageRecordService.getCount();
            //总页数
            Page pages = new Page();
            pages.setCurrentPageNo(pageIndex); // 设置当前页数
            pages.setPageSize(pageSize);       // 设置一页显示数据量
            pages.setTotalCount(totalCount);   // 设置总数据量
            int totalPageCount = pages.getTotalPageCount();
            //控制首页和尾页
            if (pageIndex > totalPageCount){
                pageIndex = totalPageCount;
                throw new RuntimeException("页码不正确");
            }
            // 分页查询方法
            storageRecords = storageRecordService.List((pageIndex-1)*pageSize,pageSize,queryGoodsName,querySupplierId,queryPayStatus);
            // 调用方法查询所有供货商信息
            supplierList = supplierService.getAll();
            // 将数据放入model中
            model.addAttribute("storageRecordList",storageRecords);  // 用户信息
            model.addAttribute("totalCount",pages.getTotalCount()); // 分页信息
            model.addAttribute("currentPageNo",pages.getPageNo());
            model.addAttribute("totalPageCount",pages.getTotalPageCount());
            model.addAttribute("queryGoodsName",queryGoodsName); // 查询条件回显
            model.addAttribute("querySupplierId",querySupplierId);
            model.addAttribute("queryPayStatus",queryPayStatus);
            model.addAttribute("supplierList",supplierList);    // 供货商数据
        }catch (Exception e){
            e.printStackTrace();
        }
        return "/storageRecord/list";
    }

    @RequestMapping("/view/{storageRecordId}")
    public String view(Model model,@PathVariable Integer storageRecordId ){
        StorageRecord storageRecord = storageRecordService.RecordList(storageRecordId);
        model.addAttribute("storageRecord",storageRecord);
        return "/storageRecord/view";
    }


    @PostMapping("/add")
    public String doAdd(Model model, HttpSession session, StorageRecord storageRecord){
        StorageRecord storageRecord1 = (StorageRecord) session.getAttribute(Constants.USER_SESSION);
        storageRecord.setCreatedUserId(storageRecord1.getId());
        storageRecord1.setCreatedTime(new Date());
        // 调用添加方法
        int add = storageRecordService.add(storageRecord);
        if (add == 1){
            return "redirect:/storageRecord/list";
        }
        return "/storageRecord/add";
    }


    // 跳转到添加页面
    @GetMapping("/toAdd")
    public String add(Model model){
        // 查询供货商的名称放到下拉框中
        List<Supplier> all = supplierService.getAll();
        model.addAttribute("supplier",all);
        return "/storageRecord/add";
    }

    @RequestMapping("/toUpdate/{storageRecordId}")
    public String toUpdate(Model model,@PathVariable Integer storageRecordId){
        // 根据id查询出要修改的信息
        StorageRecord storageRecord = storageRecordService.RecordList(storageRecordId);
        model.addAttribute("storageRecord",storageRecord);

        // 查询所用供货商信息
        List<Supplier> all = supplierService.getAll();
        model.addAttribute("supplierList",all);
        model.addAttribute("id",storageRecordId);
        return "/storageRecord/update";
    }

    @PostMapping("/update")
    public String update(Model model,Integer id,String srCode,String goodsName,double goodsCount,String goodsUnit,double totalAmount,Integer supplierId,Integer payStatus){
        StorageRecord storageRecord = new StorageRecord();
        storageRecord.setId(id);
        storageRecord.setSrCode(srCode);
        storageRecord.setGoodsName(goodsName);
        storageRecord.setGoodsCount(goodsCount);
        storageRecord.setGoodsUnit(goodsUnit);
        storageRecord.setTotalAmount(totalAmount);
        storageRecord.setSupplierId(supplierId);
        storageRecord.setPayStatus(payStatus);
        int upd = storageRecordService.update(storageRecord);
        // 查询所用供货商信息
        List<Supplier> all = supplierService.getAll();
        model.addAttribute("supplierList",all);
        if (upd == 1){
            return "redirect:/storageRecord/list";
        }
        return "/storageRecord/update";
    }
}
